package com.xu.fountain.Controller;

import com.xu.fountain.Entity.AdminInfoEntity;
import com.xu.fountain.Repository.AdminInfoRepository;
import com.xu.fountain.Util.CodeUtil;
import com.xu.fountain.Util.DateUtil;
import com.xu.fountain.Util.MD5Util;
import com.xu.fountain.Util.Result;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Message: created by XUBO on 2018/4/31
 * @Description:
 */
@RequestMapping(value="admin/")
@Controller
public class AdminController {

    Logger LOG = Logger.getLogger(AdminController.class);
    @Autowired
    private AdminInfoRepository AdminRepos;


    @RequestMapping("register")
    @ResponseBody
    public Result register(@RequestParam String username, @RequestParam String password){
        LOG.info( "用户名："+username );
        AdminInfoEntity user1 = AdminRepos.findByUser( username ) ;
        LOG.info( "用户："+user1 );
        if (user1 == null) {
            user1.setToken(CodeUtil.getCode(20) );
            user1.setPwd((MD5Util.encode(password)));
            user1.setAddtime(DateUtil.newdate());
            AdminRepos.save( user1 );
            return new Result(1,"注册成功",user1);

        }else{
            return new Result(0,"用户名已注册",user1);
        }
    }

}
